package cn.kinoko.controller.auth;

import cn.kinoko.common.base.model.BaseController;
import cn.kinoko.common.base.model.Result;
import cn.kinoko.model.auth.req.EmailCodeReq;
import cn.kinoko.model.auth.req.LoginReq;
import cn.kinoko.model.auth.req.RegisterReq;
import cn.kinoko.model.auth.req.ResetPwdReq;
import cn.kinoko.model.auth.vo.CaptchaVO;
import cn.kinoko.model.auth.vo.TokenAuthVO;
import cn.kinoko.service.auth.AuthService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * @author kinoko
 */
@RestController
@RequestMapping("/auth")
public class AuthController extends BaseController {

    @Resource
    private AuthService authService;

    @GetMapping("/captcha")
    public Result<CaptchaVO> buildCaptcha(HttpServletRequest request) {
        return success(authService.buildCaptcha(request));
    }

    @PostMapping("/register")
    public Result<Object> register(@RequestBody @Validated RegisterReq req, HttpServletRequest request) {
        return toAjax(authService.register(req, request));
    }

    @PostMapping("/sendEmailCode")
    public Result<Object> sendEmailCode(@RequestBody @Validated EmailCodeReq req, HttpServletRequest request) {
        return toAjax(authService.sendEmailCode(req, request));
    }

    @PostMapping("/resetPwd")
    public Result<Object> resetPwd(@RequestBody @Validated ResetPwdReq req) {
        return toAjax(authService.resetPwd(req));
    }

    @PostMapping("/login")
    public Result<TokenAuthVO> login(@RequestBody @Validated LoginReq loginInfo, HttpServletRequest request){
        return success(authService.login(loginInfo, request));
    }

    @GetMapping("/refresh")
    public Result<TokenAuthVO> refresh(){
        return success(authService.refresh());
    }
    
}